<?php
/**
 * Created by PhpStorm.
 * User: JYuans
 * Date: 2019/11/28
 * Time: 10:25
 */
namespace Bstage\Controller;

class AdminController extends BstageController {

    /**
     * 管理员列表查询接口
     */
    public function adminQuery(){
        //分页
        $now_page = $_REQUEST['now_page']?$_REQUEST['now_page']:1;//默认第一页
        $page_size = $_REQUEST['page_size']?$_REQUEST['page_size']:50;//默认每页50条
        //查询条件
        $where = array();
        //筛选姓名
        if($_REQUEST["search_username"]){
            $where['username'] = ['like','%'.$_REQUEST['search_username'].'%'];
        }

        $field = "admin_id,username,add_time";
        $list_data = M('Admin')
            ->where($where)
            ->field($field)
            ->order('add_time DESC')
            ->page($now_page,$page_size)
            ->select();

        if(!$list_data) {
            $this->returnJson(503,'没有更多数据');
        }

        foreach($list_data as $key=>$value){
            $list_data[$key]['add_time'] = changeDateTime($value['add_time']);
        }

        $count = M('Admin')
            ->where($where)
            ->count();

        $return_data = array();
        $return_data['list_data'] = $list_data;
        $return_data['count'] = $count;
        $return_data['now_page'] = $now_page;
        $return_data['page_size'] = $page_size;
        $return_data['max_page'] = ceil($count / $page_size);

        $this->successReturn("获取成功",$return_data);
    }

    /**
     * 添加管理员账号接口
     */
    public function addAdminAction(){
        $new_data = array();
        $new_data['username'] = $_REQUEST['username']?$_REQUEST['username']:$this->errorReturn("请输入账号名");
        $new_data['password'] = $_REQUEST['password']?md5($_REQUEST['password']):$this->errorReturn("请输入密码");
        $new_data['add_time'] = time();

        $check_username = M("Admin")
            ->where(['username'=>$new_data['username']])
            ->count();
        if($check_username){
            $this->errorReturn("该账号名已存在");
        }

        $add_result = M("Admin")
            ->add($new_data);
        if($add_result){
            $this->successReturn("添加成功");
        }else{
            $this->errorReturn("添加失败");
        }
    }

    /**
     * 获取管理员信息接口
     */
    public function getAdminDataAction(){
        $admin_id = $_REQUEST['admin_id']?$_REQUEST['admin_id']:$this->errorReturn("缺少管理员ID");

        $data = M("Admin")
            ->where(['admin_id'=>$admin_id])
            ->field('admin_id,username')
            ->find();
        if(!$data){
            $this->errorReturn("管理员ID错误");
        }

        $this->successReturn("获取成功",$data);
    }

    /**
     * 修改管理员账号接口
     */
    public function editAdminAction(){
        $admin_id = $_REQUEST['admin_id']?$_REQUEST['admin_id']:$this->errorReturn("缺少管理员ID");

        $new_data = array();
        $new_data['username'] = $_REQUEST['username']?$_REQUEST['username']:$this->errorReturn("请输入账号名");
        $new_data['update_time'] = time();

        $check_username = M("Admin")
            ->where(['username'=>$new_data['username'],'admin_id'=>['neq',$admin_id]])
            ->count();
        if($check_username){
            $this->errorReturn("该账号名已存在");
        }

        $update_result = M("Admin")
            ->where(['admin_id'=>$admin_id])
            ->save($new_data);
        if($update_result){
            $this->successReturn("修改成功");
        }else{
            $this->errorReturn("更新数据失败，请重新尝试");
        }
    }

    /**
     * 删除管理员账号接口
     */
    public function deleteAdminAction(){
        $id_str = $_REQUEST["id_str"]?$_REQUEST["id_str"]:$this->errorReturn("请选择需要删除的账号");
        $id_array = explode(",",$id_str);

        for($i=0;$i<count($id_array);$i++){
            if($id_array[$i] == 1){
                $this->errorReturn("无法删除admin账号");
            }
        }

        $delete_result = M("Admin")
            ->where(['admin_id'=>['in',$id_str]])
            ->delete();

        if($delete_result){
            $this->successReturn("删除成功");
        }else{
            $this->errorReturn("删除失败，请重新尝试");
        }
    }

    /**
     * 重置密码接口
     */
    public function editPasswordAction(){
        $admin_id = $_REQUEST['admin_id']?$_REQUEST['admin_id']:$this->errorReturn("缺少管理员ID");

        $password = $_REQUEST["password"]?md5($_REQUEST["password"]):$this->errorReturn("请输入密码");

        $update_result = M("Admin")
            ->where(['admin_id'=>$admin_id])
            ->setField("password",$password);
        if($update_result){
            $this->successReturn("重置成功");
        }else{
            $this->errorReturn("新密码不能与原密码相同");
        }
    }
}